package com.wolfpire.knowledge.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wolfpire.knowledge.service.KnowledgeService;
import com.wolfpire.profession.service.ProfessionService;
import com.wolfpire.subject.service.SubjectService;
import com.wolfpire.system.common.AuthorityUtils;
import com.wolfpire.system.common.EasyuiPage;
import com.wolfpire.system.common.EasyuiPagination;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.Result;
import com.wolfpire.system.common.springmvc.PrincipalArgument;
import com.wolfpire.system.common.util.StringUtils;
import com.wolfpire.system.model.Authority;
import com.wolfpire.system.model.DataDict;
import com.wolfpire.system.model.Knowledge;
import com.wolfpire.system.model.Org;
import com.wolfpire.system.model.Profession;
import com.wolfpire.system.model.Role;
import com.wolfpire.system.model.Subject;
import com.wolfpire.system.model.User;
import com.wolfpire.system.service.RoleService;

/**
 * @author xp
 *
 */

@Controller
@RequestMapping("/knowledge")
public class KnowledgeController {
	
	@Resource private KnowledgeService knowledgeService;
	
	@RequestMapping("/list")
	public ModelAndView list(@PrincipalArgument User user, @RequestParam Long menuId) {
		//获取对应菜单的操作权限
//		List<Authority> operAuthorities = getAperAuthorities(user, menuId);
		List<Authority> operAuthorities = AuthorityUtils.getAperAuthorities(user, menuId);
		return new ModelAndView("knowledgeManage/knowledge_list", "operAuthorities", operAuthorities);
	}
	
	@ResponseBody
	@RequestMapping("/queryList")
	public EasyuiPagination<Knowledge> queryList(EasyuiPage<Knowledge> page, Knowledge filterKnowledge) {
		if (null != filterKnowledge && null != filterKnowledge.getId()) {
			filterKnowledge.setParentId(filterKnowledge.getId());
			filterKnowledge.setId(null);
		}
		Page<Knowledge> knowledges = this.knowledgeService.setPageDataList(page, filterKnowledge);
		return new EasyuiPagination<Knowledge>(knowledges);
	}
	 
	@RequestMapping(value = "/add")
	public ModelAndView add(HttpSession session, @RequestParam(value = "subjectId", required = false) String subjectId) {
		ModelAndView mv = new ModelAndView("knowledgeManage/knowledge_add");
		mv.getModelMap().put("subjectId", StringUtils.isBlank(subjectId) ? StringUtils.EMPTY : subjectId);
		return mv;
	}
	
	@RequestMapping("/edit")
	public ModelAndView edit(Long id) {
		ModelAndView mv = new ModelAndView("knowledgeManage/knowledge_edit");
		if (null != id) {
//			User user = this.userService.get(id);
			Knowledge knowledge = this.knowledgeService.getCompleteKnowledge(id);
			mv.getModelMap().put("knowledge", knowledge);
		}
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/save")
	public Map<String, Object> save(Knowledge newKnowledge) {
		Map<String, Object> map = new HashMap<String, Object>();
		boolean result = false;
		String msg = null;
		String joinName = "";
		// 判断添加的用户是否存在
		Knowledge targetKnowledge = this.knowledgeService.getByName(newKnowledge.getName());
		if(newKnowledge.getParentId() != null && !"".equals(newKnowledge.getParentId()))
			 joinName = this.knowledgeService.get(Long.valueOf(newKnowledge.getParentId())).getName();
	   if (null == targetKnowledge) {
		   newKnowledge.setJoinname(joinName);
		  this.knowledgeService.saveOrUpdate(newKnowledge);
		  result = true;
		  msg = "新增成功";
		} else {
			// 更新
			newKnowledge.setJoinname(joinName);
			this.knowledgeService.saveOrUpdate(newKnowledge);
			result = true;
			msg = "更新成功";
		}
		map.put("result", result);
		map.put("msg", msg);
		return map;
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public Result delete(Long id) {
		try {
			this.knowledgeService.delete(id);
			return new Result(true, "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "删除失败");
		}
	}
	
	@ResponseBody
	@RequestMapping("/knowledges")
	public List<Knowledge> knowledges(Knowledge knowledge) {
		/*if (null != knowledge && null != knowledge.getId()) {
			knowledge.setParentId(knowledge.getId());
			knowledge.setId(null);
		}*/
		List<Knowledge> knowledges = this.knowledgeService.list(knowledge);
		return knowledges;
	}
}
